package com.demo.controller.flighttickets;

import com.demo.common.model.Flighttickets;
import com.demo.common.model.Flighttime;
import com.demo.service.CodeService;
import com.demo.service.FlightTimeService;
import com.demo.service.FlightticketsService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * 机票验证
 */
public class FlightticketsValidator extends Validator {

    @Inject
    FlightticketsService flightticketsService;

    @Inject
    FlightTimeService flightTimeService;

    @Inject
    CodeService codeService;

    @Override
    protected void validate(Controller controller) {
        validateRequiredString("flighttickets.flighttimeid", "flighttimeidMsg", "请选择航班编号!");

        validateRequiredString("flighttickets.tickettype", "tickettypeMsg", "请选择机票类型!");

        validateRequiredString("flighttickets.price", "priceMsg", "请输入机票单价!");

        validateRequiredString("flighttickets.num", "numMsg", "请输入机票数量!");

        validateRequiredString("flighttickets.flightticketsno", "flightticketsnoMsg", "请输入机票编号!");

        String id=controller.getPara("flighttickets.id");
        String flightticketsno=controller.getPara("flighttickets.flightticketsno");
        if(flightticketsService.findByFlightticketsno(id,flightticketsno)!=null){
            addError("flightticketsnoMsg", "机票编号已被添加，请使用别的机票编号！");
        }

    }

    @Override
    protected void handleError(Controller controller) {
        controller.keepModel(Flighttickets.class);

        String actionKey = getActionKey();
        controller.setAttr("flighttimes",flightTimeService.findByVerify());
        controller.setAttr("tickettypes",codeService.findByCategory("tickettype"));
        if (actionKey.equals("/flighttickets/save"))
            controller.render("add.html");
        else if (actionKey.equals("/flighttickets/update"))
            controller.render("edit.html");
    }
}
